package com.hmall.item.es;

import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class ElesticIndexText {

    private RestHighLevelClient client;

    // 测试索引库的连接
    @Test
    public void testIndex(){
        System.out.println("hello client = " + client);
    }
    //测试创建索引库
    @Test
    public void testCreateIndex() throws IOException {
        // 1、创建Request
        CreateIndexRequest market = new CreateIndexRequest("market");
        //2、 准备请求参数
        market.source(MAPPING_TEMPLATE, XContentType.JSON );
        //3、 发送请求
        client.indices().create(market, RequestOptions.DEFAULT);
    }

    //测试查询索引库
    @Test
    public void testGetIndex() throws IOException {
        // 1、创建Request
        GetIndexRequest market = new GetIndexRequest("market");
        //2、发送请求
//        client.indices().get(market, RequestOptions.DEFAULT); 因为JSON格式的数据解析不方便
        //要确定索引库是否存在，可以调用exists（）方法
        boolean exists = client.indices().exists(market, RequestOptions.DEFAULT);
        System.out.println("索引库是否存在：" + exists);
    }

    //测试删除索引库
    @Test
    public void testDeleteIndex() throws IOException {
        // 1、创建Request
        DeleteIndexRequest market = new DeleteIndexRequest("market");
        //2、发送请求
        client.indices().delete(market, RequestOptions.DEFAULT);
    }

    @BeforeEach// 在每个测试方法之前执行
    void setUp() {
        // 创建客户端
        this.client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://192.168.253.130:9200")
        ));
    }
    @AfterEach// 在每个测试方法之后执行
    void tearDown() throws IOException {
        // 关闭客户端
        this.client.close();
    }

    private final String MAPPING_TEMPLATE = "{\n" +
            "  \"mappings\": {\n" +
            "    \"properties\": {\n" +
            "      \"id\": {\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "      \"name\": {\n" +
            "        \"type\": \"text\",\n" +
            "        \"analyzer\": \"ik_max_word\"\n" +
            "      },\n" +
            "      \"price\": {\n" +
            "        \"type\": \"integer\"\n" +
            "      },\n" +
            "      \"image\": {\n" +
            "        \"type\": \"keyword\",\n" +
            "        \"index\": false\n" +
            "      },\n" +
            "      \"category\": {\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "      \"brand\": {\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "      \"sold\": {\n" +
            "        \"type\": \"integer\"\n" +
            "      },\n" +
            "      \"commentCount\": {\n" +
            "        \"type\": \"integer\",\n" +
            "        \"index\": false\n" +
            "      },\n" +
            "      \"isAD\": {\n" +
            "        \"type\": \"boolean\"\n" +
            "      },\n" +
            "      \"updateTime\": {\n" +
            "        \"type\": \"date\"\n" +
            "      }\n" +
            "    }\n" +
            "  }\n" +
            "}";
}
